package com.incar.chuxing.baiduMap.RoutePlanOverly;

import android.graphics.Color;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.WalkingRouteLine;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于显示步行路线的overlay，自3.4.0版本起可实例化多个添加在地图中显示
 */
public class WalkingRouteOverlay extends OverlayManager {

    private WalkingRouteLine mRouteLine = null;

    public WalkingRouteOverlay(BaiduMap baiduMap) {
        super(baiduMap);
    }

    /**
     * 设置路线数据。
     */
    public void setData(WalkingRouteLine line) {
        mRouteLine = line;
    }

    @Override
    public final List<OverlayOptions> getOverlayOptions() {
        if (mRouteLine == null) {
            return null;
        }

        List<OverlayOptions> overlayList = new ArrayList<OverlayOptions>();
        // poly line list
        if (mRouteLine.getAllStep() != null
                && mRouteLine.getAllStep().size() > 0) {
            LatLng lastStepLastPoint = null;
            for (WalkingRouteLine.WalkingStep step : mRouteLine.getAllStep()) {
                List<LatLng> watPoints = step.getWayPoints();
                if (watPoints != null) {
                    List<LatLng> points = new ArrayList<LatLng>();
                    if (lastStepLastPoint != null) {
                        points.add(lastStepLastPoint);
                    }
                    points.addAll(watPoints);
                    overlayList.add(new PolylineOptions().points(points).width(9)
                            .color(getLineColor() != 0 ? getLineColor() : Color.argb(255, 10, 101, 255)).zIndex(2));
                    lastStepLastPoint = watPoints.get(watPoints.size() - 1);
                }
            }

        }

        return overlayList;
    }


    @Override
    public List<LatLng> getRouteNodes() {
        if (mRouteLine == null) {
            return null;
        }
        List<LatLng> routeNodes = new ArrayList<LatLng>();

        // starting
        if (mRouteLine.getStarting() != null) {
            routeNodes.add(mRouteLine.getStarting().getLocation());
        }
        // terminal
        if (mRouteLine.getTerminal() != null) {
            routeNodes.add(mRouteLine.getTerminal().getLocation());
        }

        // poly line list
        if (mRouteLine.getAllStep() != null && mRouteLine.getAllStep().size() > 0) {
            LatLng lastStepLastPoint = null;
            for (WalkingRouteLine.WalkingStep step : mRouteLine.getAllStep()) {
                List<LatLng> watPoints = step.getWayPoints();
                if (watPoints != null) {
                    List<LatLng> points = new ArrayList<LatLng>();
                    if (lastStepLastPoint != null) {
                        routeNodes.add(lastStepLastPoint);
                    }
                    routeNodes.addAll(watPoints);
                    lastStepLastPoint = watPoints.get(watPoints.size() - 1);
                }
            }

        }


        return routeNodes;
    }

    public int getLineColor() {
        return 0;
    }
}
